 

package com.zdkj.subway.rca.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.subway.rca.dto.in.SwCkStationDailyReportQueryDTO;
import com.zdkj.subway.rca.entity.SwCkStationDailyReportOriginal;
import com.zdkj.subway.rca.service.SwCkStationDailyReportOriginalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zdkj.subway.rca.enums.SwCkClearRuleConstans;
 

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 报表调账-西安地铁日报表查询
 *
 * @author stw
 * @date 2020-11-21 21:38:48
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swckstationdailyreportoriginal" )
@Api(value = "swckstationdailyreportoriginal", tags = "报表调账-西安地铁日报表查询")
@Slf4j
public class SwCkStationDailyReportOriginalController {

    private final SwCkStationDailyReportOriginalService swCkStationDailyReportOriginalService;
    /**
     * 查询日报表
     * @param
     * @return
     */
    @ApiOperation(value = "查询日报表,tradeDate(日期) 必选,lineCode(线路) 必选", notes = "查询日报表,tradeDate(日期) 必选,lineCode(线路) 必选")
    @GetMapping("/listSwCkStationDailyReport" )
    public R listSwCkStationDailyReport(SwCkStationDailyReportQueryDTO dto) {
		dto.setLineCode(SwCkClearRuleConstans.DEFAULT_LINE_CODE);
		if(StringUtils.isEmpty(dto.getTradeDate()) ||  StringUtils.isEmpty(dto.getLineCode())){
            return R.failed("日期和线路必选");
        }
        List<Map<String,String>> noAuditListMap = swCkStationDailyReportOriginalService.getNoAudit(dto.getLineCode(),dto.getTradeDate());
        List<SwCkStationDailyReportOriginal> swCkStationDailyReportOriginals = swCkStationDailyReportOriginalService.listSwCkStationDailyReportOriginal(dto);
        if(CollectionUtil.isNotEmpty(noAuditListMap) && noAuditListMap.size()>0){
            List<String> stationCodeList = noAuditListMap.stream().map(m -> m.get("station_code")).collect(Collectors.toList());
            List<SwCkStationDailyReportOriginal> collect = swCkStationDailyReportOriginals.stream().filter((x) -> stationCodeList.stream().anyMatch((m) -> {
                if (x.getStationCode().equals(m)) {
                    x.setAuditStatus("0");//未审核的数据
                }
                return true;
            })).collect(Collectors.toList());
            return R.ok(collect);
        }
        return R.ok(swCkStationDailyReportOriginals);


    }


    /**
     * 导出日报表
     * @param
     * @return
     */
    @ApiOperation(value = "导出日报表,tradeDate(日期) 必选,lineCode(线路) 必选", notes = "导出日报表,tradeDate(日期) 必选,lineCode(线路) 必选")
    @GetMapping({"/export"})
    @SysLog("报表调账-西安地铁日报表查询-导出日报表")
    @PreAuthorize("@pms.hasPermission('sw_ck_station_daily_report_original_export')")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "startDate", value = "开始日期：yyyy-MM-dd", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "endDate", value = "结束日期：yyyy-MM-dd", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "lineCodes", value = "线路多选，只是一二期", dataTypeClass = String.class)
            }
    )
    public void export(String startDate, String endDate,String lineCodes, HttpServletResponse response) throws ParseException {
        lineCodes = SwCkClearRuleConstans.DEFAULT_LINE_CODE;
		boolean isDateCondition = StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate) && StringUtils.isNotEmpty(lineCodes);
        if(!isDateCondition){
            return;
        }
        swCkStationDailyReportOriginalService.export(startDate,endDate,lineCodes,response);
    }
    /**
     * 导出日报表
     * @param
     * @return
     */
//    @ApiOperation(value = "导出日报表,tradeDate(日期) 必选,lineCode(线路) 必选", notes = "导出日报表,tradeDate(日期) 必选,lineCode(线路) 必选")
//    @GetMapping({"/export"})
//    @SysLog("报表调账-西安地铁日报表查询-导出日报表")
//    @PreAuthorize("@pms.hasPermission('sw_ck_station_daily_report_original_export')")
//    public void export(SwCkStationDailyReportQueryDTO dto, HttpServletResponse response) {
//        if(StringUtils.isEmpty(dto.getTradeDate()) ||  StringUtils.isEmpty(dto.getLineCode())){
//            return;
//        }
//        swCkStationDailyReportOriginalService.export(dto,response);
//    }
}
